package com.deep.order.controller.web;

import com.deep.common.utils.PageUtils;
import com.deep.common.utils.R;
import com.deep.order.annotation.ApiController;
import com.deep.order.exception.NoStockException;
import com.deep.order.model.vo.OrderConfirmVO;
import com.deep.order.model.vo.OrderSubmitVO;
import com.deep.order.model.vo.SubmitOrderResponseVO;
import com.deep.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 订单web控制器
 *
 * @author Deep
 * @date 2022/2/14
 */
@Slf4j
@ApiController
public class OrderWebController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询当前登录用户的所有订单项
     */
    @GetMapping("/listWithItem")
    public String listUserOderItems(@RequestParam(value = "pageNum", required = false, defaultValue = "1") String pageNum, Model model) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum);
        PageUtils page = orderService.queryPageWithItem(params);

        model.addAttribute("orders", page);
        return "list";
    }

    /**
     * 去订单结算页
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVO orderConfirmVO = orderService.confirmOrder();
        model.addAttribute("confirmOrderData", orderConfirmVO);
        return "confirm";
    }

    /**
     * 提交订单
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVO vo, Model model, RedirectAttributes attributes) {
        try {
            SubmitOrderResponseVO submitOrderResponseVO = orderService.submitOrder(vo);
            if (submitOrderResponseVO.getCode() == 0) {
                // 生成订单成功
                model.addAttribute("submitOrderResp", submitOrderResponseVO);
                return "pay";
            } else {
                // 生成订单失败
                String msg = "下单失败";
                switch (submitOrderResponseVO.getCode()) {
                    case 1:
                        msg += "令牌订单信息过期，请刷新再次提交";
                        log.error("令牌订单token过期");
                        break;
                    case 2:
                        msg += "订单商品价格发生变化，请确认后再次提交";
                        log.error("订单商品价格发生变化");
                        break;
                    case 3:
                        msg += "库存锁定失败，商品库存不足";
                        log.error("库存锁定失败");
                        break;
                }
                attributes.addFlashAttribute("msg", msg);
                return "redirect:http://localhost:9000/order/toTrade";
            }
        } catch (Exception e) {
            if (e instanceof NoStockException) {
                String message = ((NoStockException) e).getMessage();
                log.error("库存锁定失败：" + message);
                attributes.addFlashAttribute("msg", message);
            }
            return "redirect:http://localhost:9000/order/toTrade";
        }
    }
}
